package com.gitee.base;

import java.util.HashMap;
import java.util.Map;

import com.gitee.constants.ResponseKey;
import com.gitee.constants.ResultStatusCode;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;

/**
 * 结果基类
 * 
 * @author 赵炎
 * @version [V1.00, 2019年1月7日]
 * @since V1.00
 */
public class Result extends HashMap<String, Object>
{
    /**
     * 
     */
    private static final long serialVersionUID = 599612061689359076L;
    
    /**
     * 构造函数
     */
    public Result()
    {
    }
    
    /**
     * 成功响应
     * 
     * @param msg
     *            成功信息
     * @return
     */
    public static Result success(String msg)
    {
        Result res = new Result();
        res.put(ResponseKey.CODE, 200);
        res.put(ResponseKey.MESSAGE, msg);
        res.put(ResponseKey.TIMESTAMP, System.currentTimeMillis());
        return res;
    }
    
    /**
     * 封装返回结果
     * 
     * @param msg
     * @param data
     * @return
     */
    public static Result success(String msg, Object data)
    {
        Result res = new Result();
        res.put(ResponseKey.CODE, ResultStatusCode.SUCCESS);
        res.put(ResponseKey.MESSAGE, msg);
        Map<String, Object> pageMap = Maps.newHashMap();
        if (data instanceof PageInfo<?>)
        {
            PageInfo<?> page = (PageInfo<?>)data;
            pageMap.put(ResponseKey.ROWS, page.getList()); // 返回数据列表
            pageMap.put(ResponseKey.PAGENUM, page.getPageNum());// 当前页
            pageMap.put(ResponseKey.PAGESIZE, page.getPageSize());// 页码大小
            pageMap.put(ResponseKey.PAGES, page.getPages()); // 总页数
            pageMap.put(ResponseKey.TOTAL, page.getTotal()); // 总条数
            res.put(ResponseKey.DATA, pageMap);
        }
        else
        {
            res.put(ResponseKey.DATA, data);
        }
        
        res.put(ResponseKey.TIMESTAMP, System.currentTimeMillis());
        return res;
    }
    
    /**
     * 返回结果集封装
     * 
     * @param data
     * @return
     */
    public static Result success(Object data)
    {
        return Result.success("请求成功", data);
    }
    
    /**
     * 成功响应
     * 
     * @return
     */
    public static Result success()
    {
        return Result.success("操作成功");
    }
    
    /**
     * 错误响应
     * 
     * @param code
     *            错误码
     * @param msg
     *            错误信息
     * @return
     */
    public static Result error(int code, String msg)
    {
        Result res = new Result();
        res.put(ResponseKey.CODE, code);
        res.put(ResponseKey.MESSAGE, msg);
        res.put(ResponseKey.TIMESTAMP, System.currentTimeMillis());
        return res;
    }
    
    /**
     * 失败响应
     * 
     * @return
     */
    public static Result error()
    {
        return Result.error(ResultStatusCode.ERROR, "操作失败");
    }
    
    /**
     * 500 系统异常响应
     * 
     * @param msg
     *            错误信息
     * @return
     */
    public static Result error(String msg)
    {
        return Result.error(ResultStatusCode.INNER_ERROR, msg);
    }
    
    /**
     * 重写方法
     * 
     * @param key
     * @param value
     * @return
     * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
     */
    @Override
    public Result put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}
